package com.app.htv;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.zndroid.widget.html.ClickableTableSpan;
import com.zndroid.widget.html.DrawTableLinkSpan;
import com.zndroid.widget.TextView.HtmlTextView;
import com.zndroid.widget.html.HtmlResImageGetter;
import com.zndroid.widget.html.OnClickATagListener;
import com.zndroid.widget.html.OnClickImgTagListener;

import static com.app.htv.WebViewActivity.EXTRA_TABLE_HTML;

public class MainActivity extends AppCompatActivity {
    private HtmlTextView htmlTextView;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        htmlTextView = findViewById(R.id.htv);
        imageView = findViewById(R.id.im);

        //text.setRemoveFromHtmlSpace(false); // default is true
        htmlTextView.setClickableTableSpan(new ClickableTableSpanImpl());
        DrawTableLinkSpan drawTableLinkSpan = new DrawTableLinkSpan();
        drawTableLinkSpan.setTableLinkText("[tap for table]");
        htmlTextView.setDrawTableLinkSpan(drawTableLinkSpan);

        // Best to use indentation that matches screen density.
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        htmlTextView.setListIndentPx(metrics.density * 10);

        // a tag click listener
        htmlTextView.setOnClickATagListener(new OnClickATagListener() {
            @Override
            public boolean onClick(View widget, String spannedText, @Nullable String href) {
                final Toast toast = Toast.makeText(MainActivity.this, null, Toast.LENGTH_SHORT);
                toast.setText(href);
                toast.show();

                return false;
            }
        });
        htmlTextView.setOnClickImgTagListener(new OnClickImgTagListener() {
            @Override
            public void onClick(String source, Drawable drawable) {
                final Toast toast = Toast.makeText(MainActivity.this, null, Toast.LENGTH_SHORT);
                toast.setText(source);
                toast.show();

                htmlTextView.setVisibility(View.GONE);
                imageView.setImageDrawable(drawable);
            }
        });
        htmlTextView.blockQuoteBackgroundColor = getResources().getColor(R.color.whitish);
        htmlTextView.blockQuoteStripColor = getResources().getColor(R.color.blue);
        htmlTextView.setHtml(R.raw.example, new HtmlResImageGetter(this));
    }

    class ClickableTableSpanImpl extends ClickableTableSpan {
        @Override
        public ClickableTableSpan newInstance() {
            return new ClickableTableSpanImpl();
        }

        @Override
        public void onClick(View widget) {
            Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
            intent.putExtra(EXTRA_TABLE_HTML, getTableHtml());
            startActivity(intent);
        }
    }
}